package com.lintesis.lintesisview.webgui.users;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Listheader;
import org.zkoss.zul.Textbox;

import com.lintesis.lintesismodel.common.hibernate.BussinessObject;
import com.lintesis.lintesismodel.common.hibernate.HibernateSearchObject;
import com.lintesis.lintesismodel.users.Role;
import com.lintesis.lintesismodel.users.User;
import com.lintesis.lintesisview.common.ListCtrl;
import com.lintesis.lintesisview.common.menu.UserWorkspace;
import com.lintesis.lintesismodel.common.hibernate.GenericDAO;


public class UserListCtrl  extends ListCtrl<User>{
	
	private static final long serialVersionUID = 922482947086338699L;

	Textbox tbLogin;
	Textbox tbName;
	Textbox tbSurname;
	Checkbox chEnabled;
	
	Listheader lhLogin; 
	Listheader lhName; 
	Listheader lhSurname;
	Listheader lhEnabled;
	
	
	@Override
	protected void doDefineOrders() {
		orders.put("login", lhLogin);
		orders.put("name", lhName);
		orders.put("surname", lhSurname);
		orders.put("userState", lhEnabled);
	}

	@Override
	public void doSetSearchComponents() {
		searchComponents.add(tbLogin);
		searchComponents.add(tbName);
		searchComponents.add(tbSurname);
		searchComponents.add(chEnabled);
	}

	@Override
	protected String getABMDialogURL() {
		return "/pages/user/userDialog.zul";
	}

	@Override
	protected BussinessObject getNewEntidad() {
		User user = new User();		
		return user;
	}
	
	@Override
	protected HibernateSearchObject<User> getNewHibernateSearchObject()
	{
			return new HibernateSearchObject<User>(User.class);
	}

	@Override
	protected void setDescripcionLarga() {
		// TODO Auto-generated method stub
	}

	@Override
	protected void setSearchFilters() {
		filters.put("login", "%"+tbLogin.getText()+"%");
		filters.put("name", "%"+tbName.getText()+"%");
		filters.put("surname", "%"+tbSurname.getText()+"%");
		if(!checkShowAll.isChecked())
			filters.put("userState", chEnabled.isChecked());
	}
	
	@Override
	public String getEntityName()
	{
		return "SysUser";
	}

	@Override
	protected void setDefaultFilters() { 
		try {
			List<User> lu = new GenericDAO<User>(UserWorkspace.getInstance().gethSession()).getByBussinessObject(new User());
			List<Role> lr = new GenericDAO<Role>(UserWorkspace.getInstance().gethSession()).getByBussinessObject(new Role());
			List<Role> aux = new ArrayList<Role>();
			List<Role> aux1 = new ArrayList<Role>();
			List<User> auxUser = new ArrayList<User>();
//			List<Company> lc = UserWorkspace.getInstance().getUser().getAllowedCompanies();		
//			List<Station> ls = UserWorkspace.getInstance().getUser().getAllowedStations();	
			//filtro los roles que tiene asignado este usuario por company y station
//			for(Role r: lr){
//				List<Company> lcr = r.getCompanies();
//				for(Company c: lcr){
//					if(lc.indexOf((Object)c) != -1){
//						aux.add(r);
//					}
//				}
//			}
//			for(Role r: lr){
//				List<Station> lcr = r.getStations();
//				for(Station c: lcr){
//					if(ls.indexOf((Object)c) != -1){
//						aux1.add(r);
//					}
//				}
//			}				
			for(Role r: aux1){
				if(aux.indexOf(r) == -1){
					aux.add(r);
				}
			}
			// de la lista completa de usuarios se toman solos aquellos que comparten los mismos roles tanto por company y station
			for(User u: lu){
				List<Role> lru = u.getRols();
				for(Role r: lru){
					if(aux.indexOf((Object)r) != -1){
						auxUser.add(u);
					}
				}
			}			
			getHibernateSearchObject().addSortAsc("name");
			getHibernateSearchObject().addFilterIn(null, auxUser);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		}
	}
	
//	protected void setDefaultFilters(){
//		System.out.println("Inicia Método: "+Calendar.getInstance().getTime());
//		getHibernateSearchObject().clearFilters();
//		//getHibernateSearchObject().setDistinct(true);
//		if(UserWorkspace.getInstance().getUser().getAllowedCompanies().size()>0)        	
//			getHibernateSearchObject().addFilterSome("rols.companies", Filter.in("", UserWorkspace.getInstance().getUser().getAllowedCompanies()));
//		if(UserWorkspace.getInstance().getUser().getAllowedStations().size()>0)
//			getHibernateSearchObject().addFilterSome("rols.stations", Filter.in("", UserWorkspace.getInstance().getUser().getAllowedStations()));
//		System.out.println("Finaliza Método: "+Calendar.getInstance().getTime());
//	}
	
}
